package com.hh.eduorder.controller;


import com.hh.commonutils.Result;
import com.hh.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author huhuan
 * @since 2022-02-20
 */
@RestController

@RequestMapping("/eduorder/paylog")
public class PayLogController {
    @Autowired
    private PayLogService payLogService;

    @GetMapping("createNative/{orderNo}")
    public Result createNative(@PathVariable("orderNo") String orderNo){
        HashMap<String, Object> map = payLogService.createPayOrder(orderNo);
        return Result.success().data(map);
    }

    @GetMapping("queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable("orderNo") String orderNo){
        Map paySuccess = payLogService.queryPayStatus(orderNo);
        System.out.printf(paySuccess.toString());


        if (paySuccess == null || !"SUCCESS".equals(paySuccess.get("trade_state"))) {
            payLogService.updatePayLogStatus(paySuccess,0);
            return Result.fail();
        } else {
            payLogService.updatePayLogStatus(paySuccess,1);
            return Result.success();
        }
    }
}

